package com.poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriteTest {
    String PATH = "E:\\code\\after-class study\\POI\\PoiTest\\";

    @Test
    public void testWriteExcel03(){
        //1,创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //2,创建一个工作表
        Sheet sheet = workbook.createSheet("测试excel");
        //3，创建一个行
        Row row1 = sheet.createRow(0);
        //4，创建一个单元格
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("今日新增观众");
        Cell cell2 = row1.createCell(1);
        cell2.setCellValue(88);
        //第二行
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

        //生成一张表，其实是流操作
        //jdk1.7 对流的写法，创建流的步骤放在try()里，执行完try{}里的代码之后会自动关闭流，因为
        //try()里面的对象实现了AutoCloseable接口
        try(FileOutputStream fos = new FileOutputStream(PATH+"观众统计报表03.xls")){
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        }

//       FileOutputStream fileOutputStream = null;
//        try {
//            fileOutputStream = new FileOutputStream(PATH + "观众统计报表03.xls");
//            workbook.write(fileOutputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            //关闭流
//            try {
//                fileOutputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
        System.out.printf("输出完毕");
    }

    @Test
    public void testWriteExcel07(){
        //1，创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        //2,创建一张工作表
        Sheet sheet = workbook.createSheet("学生信息统计");
        //3,创建行
        Row row1 = sheet.createRow(0);
        Row row2 = sheet.createRow(1);
        //4，创建列，单元格
        //4.1 第一行
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("序号");
        Cell cell2 = row1.createCell(1);
        cell2.setCellValue("学生姓名");
        Cell cell3 = row1.createCell(2);
        cell3.setCellValue("年龄");
        //4.2 第二行
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue(001);
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue("刘梦会");
        Cell cell23 = row2.createCell(2);
        cell23.setCellValue(21);
        //指定路径生成一张表,其实是流操作
        try(FileOutputStream fos = new FileOutputStream(PATH+"学生信息表07.xlsx"))
        {
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.printf("生成表成功");
    }



}
